//
//  AppDelegate.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2020/8/3.
//  Copyright © 2020 叁拾叁. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var canAllButUpsideDown: Bool = false
    var wxDelegate = WXDelegate()

    var navc:SYBaseNavigationController!
    var webLoginVC = HSWebViewController(path: kWebUrl, isLoginPage: true)
    var tabbarVC: SYBaseTarBarViewController!
    var loginVC = SYAccountLoginViewController()
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.enableAutoToolbar = true
        window = UIWindow(frame: UIScreen.main.bounds)
        
        self.checkToken()

        window!.rootViewController = navc
        window!.makeKeyAndVisible()
        _ = self.sy_application(application, didFinishLaunchingWithOptions: launchOptions)
        return true
    }

   
    // 限制在特定页面才可以横屏
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if canAllButUpsideDown {
            return UIInterfaceOrientationMask.allButUpsideDown
        } else {
            return UIInterfaceOrientationMask.portrait
        }
        _ = self.sy_application(application, supportedInterfaceOrientationsFor: window)
    }

    func checkToken() {
        
        let token = UserDefaults.standard.value(forKey: "userToken") as? String ?? ""
        if !token.isEmpty {
            let roleId = UserDefaults.standard.value(forKey: "roleId") as? String ?? ""
            tabbarVC = SYBaseTarBarViewController(roleId: roleId)
            navc =  SYBaseNavigationController (rootViewController: tabbarVC)
        } else {
            navc =  SYBaseNavigationController (rootViewController: loginVC)
        }
    }
}

